<?php
/**
 * Display My Queries
 *
 * @author Xavier VILAIN
 * @package Fox\Queries
 * @license
 *
 */

use Fox\Html\Screen;
use Fox\Query\Messages;
use Fox\Grid\FieldCat\Field\Renderer\Anchor;
use Fox\Html\Block;
use Fox\Generator;
use Fox\Grid;
use Fox\Dao\SqlFragment;
use Fox\Query;

$xview = Xview::instance();
$showHidden = $xview->val('showhidden',false);
$usrid = User::current()->getId();
$finder = new Query();
$queries = $finder->find(new SqlFragment("private = ? OR cname = ? OR uname = ? ",array(0,$usrid,$usrid)),array('label' => false));
if(!$showHidden) {
	foreach($queries as $id => $query) {
		$userParam = $query->userParam();
		if(!empty($userParam) && $query->userParam()->hidden) {
			unset($queries[$id]);
		}
	}
}

$grid = new Grid('gQueries',Generator::url('queries'));
$grid->fieldcat(array(array('query_label','label'),'idele'));
$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()->field('label')->renderers(new Anchor(
		Generator::location(Generator::useFunction(function($data){
		/* @var $data Fox\Query */
		return Generator::generateValue($data->location(),$data);
	}))
));
$grid->fieldcat()->setGenerators(array(
	'idele' => Generator::display('remove',Generator::url('queries','remove',null,array('queryid' => Generator::useAttribute('queryid'))))
	)
);
$grid->initialize($queries);
$showHidden = ($showHidden) ? false : true;
$link = Generator::generateValue(Generator::ajaxReload("MC_gQueries",null,null,"'&showhidden=$showHidden'"),"");
$grid->addActionLine('<a href="#" onclick="'.$link.'" class="link">'.Msg(Hive::BUNDLE,Hive::SHOW_MORE).'</a>');

if($xview->isPrintMode() || $xview->ajax()) {
	return $grid->display();
}
else
{
	$screen = new Screen("sQueries",null);
	$screen->pageTitle(Msg(Messages::BUNDLE,Messages::LABEL_MY_QUERIES));
	$screen->add($grid);
	echo $screen->display();
}
